<?php
namespace App\Adshopuser\Controllers;
use App\Adshopuser\Models\BShopExpress;
use App\Adshopuser\Models\BShopOrder;
use App\Adshopuser\Models\BShopOrderCount;
use App\Adshopuser\Models\BShopOrderExpress;
use App\Adshopuser\Models\BShopOrderRefund;
use Common\ControllerAdshopBase;
use Common\Models\BShopOrderAddress;

class ShoporderController extends ControllerAdshopBase
{

    public function initialize()
    {
        parent::initialize();
    }


    public function orderlistAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $this->params['limit']=10;
        }
        if(!isset($this->params['keywords']) || empty($this->params['keywords'])){
            $this->params['keywords']="";
        }
        $BShopOrderDb = new BShopOrder();
        $get_list = $BShopOrderDb->admin_get_list($this->params);
        $this->ok($get_list);
    }



    public function orderdetailAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }
        $BShopOrderDb = new BShopOrder();
        $get_list = $BShopOrderDb->get_order_detail($this->params['order_sn']);
        $this->ok($get_list);
    }


    /**
     * 修改地址
     */
    public function update_addressAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }
        $BShopOrderDb= new BShopOrder();
        $shop_order = $BShopOrderDb->findFirstByOrderSn($this->params['order_sn']);
        if(!$shop_order){
            $this->fail(-1,'订单不存在');
        }
        $BShopOrderAddressDb = new BShopOrderAddress();
        $shop_order_address = $BShopOrderAddressDb->findFirstById($shop_order->address_id);
        if(!$shop_order_address){
            $this->fail(-1,'没有找到地址');
        }
        $shop_order_address->name = $this->params['name'];
        $shop_order_address->phone = $this->params['phone'];
        $shop_order_address->address = $this->params['address'];
        if($shop_order_address->save()){
            $this->ok([]);
        }
        $this->fail(-1,'修改失败');

    }


    /**
     * 修改价格
     */
    public function update_priceAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }
        $BShopOrderDb= new BShopOrder();
        $shop_order = $BShopOrderDb->findFirstByOrderSn($this->params['order_sn']);
        if(!$shop_order){
            $this->fail(-1,'订单不存在');
        }
        $shop_order->end_price = $this->params['end_price'];
        $shop_order->tips = $shop_order->tips."|后台修改价格:".$this->params['end_price'];
        if($shop_order->save()){
            $this->ok([]);
        }
        $this->fail(-1,'修改失败');
    }


    /**
     * 取消订单
     */
    public function cancle_orderAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }

        $this->dbMaster->begin();

        $BShopOrderDb= new BShopOrder();
        $shop_order = $BShopOrderDb->findFirstByOrderSn($this->params['order_sn']);
        if(!$shop_order){
            $this->fail(-1,'订单不存在');
        }
        $shop_order->status = -1;
        $shop_order->tips = $shop_order->tips."|后台取消订单";
        if(!$shop_order->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'取消失败!');
        }

        $BShopOrderCountDb = new BShopOrderCount();
        $shop_order_count = $BShopOrderCountDb->findFirstByOrderSn($shop_order->order_count_sn);
        $shop_order_count->order_status = -1;
        if(!$shop_order_count->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'取消失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }



    /**
     * 取消订单
     */
    public function pay_orderAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }

        $this->dbMaster->begin();

        $BShopOrderDb= new BShopOrder();
        $shop_order = $BShopOrderDb->findFirstByOrderSn($this->params['order_sn']);
        if(!$shop_order){
            $this->fail(-1,'订单不存在');
        }
        $shop_order->status = 1;
        $shop_order->tips = $shop_order->tips."|后台设置为已付款订单";
        if(!$shop_order->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'取消失败!');
        }

        $BShopOrderCountDb = new BShopOrderCount();
        $shop_order_count = $BShopOrderCountDb->findFirstByOrderSn($shop_order->order_count_sn);
        $shop_order_count->order_status = 1;
        if(!$shop_order_count->save()){
            $this->dbMaster->rollback();
            $this->fail(-1,'取消失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }


    /**
     * 选择快递公司
     */
    public function express_companyAction(){
        $BShopExpressDb = new BShopExpress();
        $get_list = $BShopExpressDb->find();
        $this->ok($get_list);
    }


    /**
     * 发货
     */
    public function add_order_expressAction(){
        if(!isset($this->params['order_sn']) || empty($this->params['order_sn'])){
            $this->fail(-1,'订单号不能为空');
        }
        if(!isset($this->params['express_id']) || empty($this->params['express_id'])){
            $this->fail(-1,'请选择快递公司');
        }
        if(!isset($this->params['express_sn']) || empty($this->params['express_sn'])){
            $this->fail(-1,'快递单号不能为空');
        }
        $BShopOrderDb = new BShopOrder();
        $shop_order = $BShopOrderDb->findFirstByOrderSn($this->params['order_sn']);
        if(!$shop_order){
            $this->fail(-1,'没有找到单号');
        }
        $BShopExpressDb = new BShopExpress();
        $shop_express = $BShopExpressDb->findFirstById($this->params['express_id']);
        if (!$shop_express) {
            $this->fail(-1, '没有找到快递');
        }
        if(empty($shop_order->express_id)) {
            $this->dbMaster->begin();
            $BShopOrderExpressDb = new BShopOrderExpress();
            $BShopOrderExpressDb->user_id = $shop_order->user_id;
            $BShopOrderExpressDb->express_sn = $this->params['express_sn'];
            $BShopOrderExpressDb->express_name = $shop_express->express_name;
            $BShopOrderExpressDb->express_logo = $shop_express->express_logo;
            $BShopOrderExpressDb->express_id = $shop_express->id;
            $BShopOrderExpressDb->created_at = date('Y-m-d H:i:s');
            $BShopOrderExpressDb->order_sn = $shop_order->order_sn;
            if (!$BShopOrderExpressDb->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '发货失败');
            }

            $shop_order->express_id = $BShopOrderExpressDb->id;
            $shop_order->status = 2;
            $shop_order->updated_at = date('Y-m-d H:i:s');
            if (!$shop_order->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '发货失败');
            }

            $BShopOrderCountDB = new BShopOrderCount();
            $shop_order_count = $BShopOrderCountDB->findFirstByOrderSn($shop_order->order_count_sn);
            $shop_order_count->order_status = 2;
            $shop_order_count->updated_at = date('Y-m-d H:i:s');
            if (!$shop_order_count->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '发货失败');
            }
            $this->dbMaster->commit();
            $this->ok([]);
        }else{
            $BShopOrderExpressDb = new BShopOrderExpress();
            $shop_order_express = $BShopOrderExpressDb->findFirstById($shop_order->express_id);
            if(!$shop_order_express){
                $this->fail(-1,'没有找到发货信息');
            }
            $shop_order_express->express_sn = $this->params['express_sn'];
            $shop_order_express->express_name = $shop_express->express_name;
            $shop_order_express->express_logo = $shop_express->express_logo;
            if($shop_order_express->save()){
                $this->ok([]);
            }
            $this->fail(-1, '发货失败');
        }
    }



    /**
     * 退换货
     */
    public function refundlistAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = 1;
        }
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $this->params['limit']=10;
        }
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->params['type']="1";
        }
        $BShopOrderRefundDb = new BShopOrderRefund();
        $get_list = $BShopOrderRefundDb->admin_get_list($this->params);
        $this->ok($get_list);
    }


}
